package com.gitee.qdbp.jdbc.test.tags;

import java.io.IOException;
import com.gitee.qdbp.able.enums.WrapMode;
import com.gitee.qdbp.jdbc.api.SqlBoot;
import com.gitee.qdbp.jdbc.plugins.SqlDialect;
import com.gitee.qdbp.jdbc.plugins.impl.ColumnNamingConverter;
import com.gitee.qdbp.jdbc.sql.SqlBuffer;
import com.gitee.qdbp.jdbc.sql.SqlTools;
import com.gitee.qdbp.jdbc.test.condition.DeptIsolationBuilder;
import com.gitee.qdbp.jdbc.test.condition.DeptIsolationWhere;
import com.gitee.qdbp.staticize.exception.TagException;
import com.gitee.qdbp.staticize.tags.base.BaseTag;
import com.gitee.qdbp.staticize.tags.base.NextStep;

/**
 * 生成部门数据权限隔离条件的标签
 *
 * @author zhaohuihua
 * @version 20201013
 * @since 3.2.0
 */
public class DeptIsolationTag extends BaseTag {

    private String prefix;
    private String suffix;
    /** 是否添加括号 **/
    private WrapMode brackets;
    /** 用户隔离字段 **/
    private String userColumn;
    /** 代办人隔离字段 **/
    private String agentColumn;
    /** 部门隔离字段 **/
    private String deptColumn;

    private final SqlBoot sqlBoot;

    public DeptIsolationTag(SqlBoot sqlBoot) {
        this.sqlBoot = sqlBoot;
    }

    @Override
    public NextStep doHandle() throws TagException, IOException {
        SqlDialect dialect = this.getStackValue("db.dialect", SqlDialect.class);
        if (dialect == null) {
            throw new TagException("Context variable of '${db.dialect}' is null");
        }

        DeptIsolationWhere condition = new DeptIsolationWhere();
        condition.setDeptField(deptColumn);
        condition.setUserField(userColumn);
        condition.setAgentField(agentColumn);
        DeptIsolationBuilder builder = sqlBoot.plugins().getWhereSqlBuilder(DeptIsolationWhere.class);
        SqlBuffer sql = builder.buildSql(condition, new ColumnNamingConverter(sqlBoot.plugins()), sqlBoot);

        if (sql == null || sql.isBlank()) {
            return NextStep.SKIP_BODY;
        }

        String leadingBlank = clearLeadingBlank();
        if (leadingBlank != null) {
            sql.shortcut().pd(leadingBlank);
        }
        SqlTools.wrap(sql, brackets, prefix, null, suffix, null);

        this.print(sql);
        return NextStep.SKIP_BODY;
    }

    public String getPrefix() {
        return prefix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }

    public WrapMode getBrackets() {
        return brackets;
    }

    public void setBrackets(String brackets) {
        this.brackets = WrapMode.of(brackets, "brackets value");
    }

    public String getUserColumn() {
        return userColumn;
    }

    public void setUserColumn(String userColumn) {
        this.userColumn = userColumn;
    }

    public String getDeptColumn() {
        return deptColumn;
    }

    public void setDeptColumn(String deptColumn) {
        this.deptColumn = deptColumn;
    }

    public String getAgentColumn() {
        return agentColumn;
    }

    public void setAgentColumn(String agentColumn) {
        this.agentColumn = agentColumn;
    }
}
